package com.wzdigit.wms.raw.service.backtosap;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.domain.basic.Sku;
import com.wzdigit.wms.basic.domain.basic.Warehouse;
import com.wzdigit.wms.basic.domain.mapping.CodeMapping;
import com.wzdigit.wms.basic.domain.mid.IfPostback;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.enums.writeback.BackResultEnum;
import com.wzdigit.wms.basic.enums.writeback.OtherSystemEnum;
import com.wzdigit.wms.basic.enums.writeback.TopicEnum;
import com.wzdigit.wms.basic.mapper.core.basic.SkuMapper;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseMapper;
import com.wzdigit.wms.basic.service.code.CodeMappingService;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.common.utils.SapWebServiceUtil;
import com.wzdigit.wms.raw.client.response.gdn.delivery.DeliveryGoodsResponse;
import com.wzdigit.wms.raw.client.response.gdn.delivery.SimpleGdnDtl;
import com.wzdigit.wms.raw.domain.mid.PoDtl;
import com.wzdigit.wms.raw.domain.mid.PoHeader;
import com.wzdigit.wms.raw.exception.SystemException;
import com.wzdigit.wms.raw.service.mid.IfPostbackService;
import com.wzdigit.wms.web.service.common.wms2sap.ZDT_MsgHeader;
import com.wzdigit.wms.web.service.wms2sap.waitcheckinstore.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.*;

/*
 * 待检入库回写
 *
 * @author shuaic
 * @date 2021/6/18 9:28
*/
@Service
@Slf4j
public class WaitCheckBackService {

	@Autowired
	private IfPostbackService ifPostbackService;

	@Autowired
	private CodeMappingService codeMappingService;

	@Autowired
	private WarehouseMapper warehouseMapper;
	
	@Autowired
	private SkuMapper skuMapper;
	
	@Value("${sap.web.service.protocol-head}")
	private String protocolHead;

	@Value("${sap.web.service.waitCheckInstore-url}")
	private String uri;

	@Value("${wzdigit.web.service.sap.ip}")
	private String ip;

	/**
	 * 回传给SAP
	 * 
	 * @param list
	 * @return java.lang.String
	 * @author shuaic
	 * @date 2021/8/2 19:49
	 */
	public Map<String, String> backToSap(List<ZDT_064_ReqRECORDS> list) throws RemoteException {
		log.info("调用SAP待检入库接口开始--------");
		log.info("发送的报文===》" + list);
		
		String urlString = protocolHead + ip + uri;
		
		ZDT_064_Req req = new ZDT_064_Req();
		
		ZDT_MsgHeader header = SapWebServiceUtil.getHeader();
		req.setHEADER(header);
		
		ZDT_064_ReqRECORDS[] records = list.toArray(new ZDT_064_ReqRECORDS[list.size()]);
		req.setRECORDS(records);
		
		ZSI_064_WMS2SAP_OutProxy proxy = new ZSI_064_WMS2SAP_OutProxy();
		proxy.setEndpoint(urlString);
		
		ZDT_064_Rsp rsp = null;// 回参
		try {
			// 调接口
			rsp = proxy.ZSI_064_WMS2SAP_Out(req);
			log.info("回传的报文===》" + JSON.toJSONString(rsp));
			log.info("调用SAP待检入库接口结束--------");
		} catch (Exception e) {
			log.info("调用SAP待检入库接口异常--------");
			e.printStackTrace();
			throw e;
		}
		
		if ("S".equals(rsp.getHEADER().getSTATUS())) {
			Map<String, String> map = new HashMap<>();
			for (ZDT_064_RspRECORDS item : rsp.getRECORDS()) {
				map.put(item.getWMSID(), item.getQPLOS());	// 获取返回的检验批次
			}
			return map;
		} else {
			String msg = rsp.getHEADER().getMESSAGE();
			if(StringUtils.isBlank(msg)) {
				msg = "处理失败";
			}
			throw new SystemException(String.format("SAP反馈：%s", msg));
		}
	}


	/**
	 * 异步回传给SAP
	 *
	 * @param list
	 * @return java.lang.String
	 * @author shuaic
	 * @date 2021/8/2 19:49
	 */
	public void backToSapSync(List<ZDT_064_ReqRECORDS> list,String asnNo)  {
		log.info("调用SAP待检入库接口开始--------");
		log.info("发送的报文===》" + list);
		try {
			String jsonString = JSON.toJSONString(list);

			//插入数据到待回传数据表
			IfPostback ifPostback = new IfPostback();
			ifPostback.setDestSystem(OtherSystemEnum.SYSYTEM_SAP.getEnumName());
			ifPostback.setPostbackNo(asnNo);
			ifPostback.setPostbackTopic(TopicEnum.TOPIC_SAP_WAIT_CHECK_IN_STORE.getEnumName());
			ifPostback.setPostbackContent(jsonString);
			ifPostback.setPostbackResult(BackResultEnum.DEFAULT_STATE.getCode());
			ifPostback.setCreateTime(new Date());
			ifPostback.setDestSystem(OtherSystemEnum.SYSYTEM_SAP.getEnumName());
			ifPostbackService.addOne(ifPostback);//保存
		} catch (Exception e) {
			e.printStackTrace();
			Shift.fatal(StatusCode.SERVER_MODIFY_ERROR);
		}


	}








}
